package com.ncs.cns.business.util;

import java.util.Date;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ncs.cns.business.to.UserNcs;

public class JsonConvertor {
	private static JsonConvertor jsonConvertor ;
	public static JsonConvertor getInstance(){
		if(jsonConvertor==null){
			createInstance();
		}
		return jsonConvertor;
	}
	
	private JsonConvertor(){
		
	}
	
	private static synchronized JsonConvertor createInstance(){
		if(jsonConvertor==null){
			jsonConvertor = new JsonConvertor();
		}
		return jsonConvertor;
	}
	
	
	
//	public UserNcs convertJson2User(String json){
//		
//		
//	}
	
	public String conver2JsonStr(Object object){
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create(); 
		String jsonStr = gson.toJson(object);
		return jsonStr;
	}
	
	public Object convert2Object(String jsonStr , Class clazz){
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create(); 
		return gson.fromJson(jsonStr, clazz);
	}
	

	public static void main(String[] args) {
		UserNcs user = new UserNcs();
		user.setUserId("haowenhao");
		user.setCreatedDate(new Date());
		String jsonStr = new JsonConvertor().conver2JsonStr(user);
		System.out.println(jsonStr);
		user = (UserNcs) new JsonConvertor().convert2Object(jsonStr, UserNcs.class);
		System.out.println(user.getCreatedDate());
	}

}
